package com.mybatis.demo.demo1;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.util.MyBatisSqlSessionUtil;

public class Test {

//	@org.junit.Test
//	public void TestXmlFun() {
//		String resource = "mybatis-config.xml";
//		InputStream inputStream;
//		SqlSessionFactory sqlSessionFactory;
//		try {
//			inputStream = Resources.getResourceAsStream(resource);
//			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//			SqlSession sqlSession = sqlSessionFactory.openSession();
//		SqlSession sqlSession = MyBatisSqlSessionUtil.getSqlSessionFactory().openSession();
//		try{
//			Students studentAdd = new Students(null, "李二狗", 5.00);
//			//新增数据
//			int iid = sqlSession.insert("com.mybatis.demo.demo1.StudentsMapper.addStudent", studentAdd);
//			sqlSession.commit();
//			System.out.println("iid==="+iid);
//			System.out.println("insertiid===="+studentAdd.getIid());
//			//查找
//			Students student = sqlSession.selectOne("com.mybatis.demo.demo1.StudentsMapper.findStudents", 1);
//			System.out.println(student.getName());
//			//update
//			Students studentModify = studentAdd;
//			studentModify.setName("厉害了我的伟哥啊");
//			int updateIid = sqlSession.update("com.mybatis.demo.demo1.StudentsMapper.updateStudent", studentModify);
//			sqlSession.commit();
//			System.out.println("update====="+updateIid);
//			//delete 
//			int delNum = sqlSession.delete("com.mybatis.demo.demo1.StudentsMapper.removeStudent", 1);
//			sqlSession.commit();
//			System.out.println("delnum===="+delNum);
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			sqlSession.close();
//		}
//	}
	
//	@org.junit.Test
//	public void TestMapperFun(){
//		String resource = "mybatis-config.xml";
//		InputStream inputStream;
//		SqlSessionFactory sqlSessionFactory;
//		try {
//			inputStream = Resources.getResourceAsStream(resource);
//			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//			SqlSession sqlSession = sqlSessionFactory.openSession();
//			StudentsMapper mapper = sqlSession.getMapper(StudentsMapper.class);
//			Students student = mapper.findStudents(1);
//			System.out.println("iid==="+student.getIid());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	@org.junit.Test
	public void addTest(){
		StudentsDAO studentsDao = new StudentsDAOImpl();
		Students students = new Students(null, "维尔港", 8.88);
		int iid = studentsDao.add(students);
		System.out.println(iid);
	}
	
	@org.junit.Test
	public void selectTest(){
		StudentsDAO studentsDao = new StudentsDAOImpl();
		Students students = studentsDao.find(2);
		System.out.println("iid===="+students.getIid()+"   name==="+students.getName()+"   sal==="+students.getSal());
	}
	
	@org.junit.Test
	public void update(){
		Students students = new Students(2, "嘻嘻嘻", 5.55);
		StudentsDAO studentsDao = new StudentsDAOImpl();
		int num = studentsDao.modify(students);
		System.out.println("num==="+num);
	}
	
	@org.junit.Test
	public void delete(){
		StudentsDAO studentsDao = new StudentsDAOImpl();
		int num = studentsDao.delete(4);
		System.out.println("delnum===="+num);
	}
	
	@org.junit.Test
	public void addMultiTest(){
		List<Students> studentsList = new ArrayList<Students>();
		StudentsDAO studentsDao = new StudentsDAOImpl();
		Students students1 = new Students(null, "维尔港", 8.88);
		Students students2 = new Students(null, "维尔港1", 9.88);
		studentsList.add(students1);
		studentsList.add(students2);
		int num = studentsDao.addMulti(studentsList);
		System.out.println("addmultinum===="+num);
	}
	
	@org.junit.Test
	public void findStudents1(){
		StudentsDAO studentsDao = new StudentsDAOImpl();
		List<Map<String, Object>> list = studentsDao.findStudents1();
		System.out.println(list.size());
		for(Map<String, Object> map : list){
			for(Map.Entry<String, Object> m : map.entrySet()){
				System.out.print(m.getKey()+"==="+String.valueOf(m.getValue())+"  ");
			}
			System.out.println("");
		}
	}
	
	@org.junit.Test
	public void findStudents2(){
		StudentsDAO studentsDao = new StudentsDAOImpl();
		List<Students> studentsList = studentsDao.findStudents2();
		System.out.println(studentsList.size());
		for(Students students : studentsList){
			System.out.println("iid=="+students.getIid()+"  name===="+students.getName()+"  sal==="+students.getSal());
		}
	}

}
